from itsdangerous import TimedJSONWebSignatureSerializer as Serializer,SignatureExpired,BadSignature
from django.conf import settings
def generic_access_token(openid):

    #1.创建序列化器
    serializer = Serializer(settings.SECRET_KEY,3600)
    #2.组织数据
    data = {'openid':openid}
    #3.对数据进行加密
    token = serializer.dumps(data)

    return token.decode()


def check_access_token(token):

    #1.创建序列化器
    serializer = Serializer(settings.SECRET_KEY,3600)
    #2.进行解密,可能会出异常
    try:
        result = serializer.loads(token)
    except BadSignature:
        return None
    else:
        return result.get('openid')